﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WOWEquipOptimizer
{
    /// <summary>
    /// the nice graph control ;)
    /// </summary>
    public partial class GraphControl : Control
    {
        private List<float> m_Values = new List<float>();
        private float m_Max = 0f;
        private float m_Min = 0f;

        public List<float> Values
        {
            get { return m_Values; }
            set { m_Values = value; }
        }
        private void calcMinMax()
        {
            float[] array = m_Values.ToArray();
            Array.Sort(array);
            m_Min = array[0];
            m_Max = array[array.Length - 1];
            if (m_Min == m_Max) m_Min = m_Max - 0.1f;
        }
        public void Reset()
        {
            m_Values = new List<float>();
            m_Max = 0;
            m_Min = 0;
            Invalidate();
        }
        public GraphControl()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            Rectangle panelRectangle = new Rectangle(this.Left, this.Top, this.Right, this.Bottom);
            Rectangle drawRectangle = new Rectangle(panelRectangle.X + 10, panelRectangle.Y + 10, panelRectangle.Width - 10, panelRectangle.Height - 10);
            pe.Graphics.FillRectangle(Brushes.WhiteSmoke, panelRectangle);
            pe.Graphics.DrawLine(new Pen(Brushes.Black, 2), new Point(drawRectangle.X,drawRectangle.Y),new Point(drawRectangle.X,drawRectangle.Height));
            pe.Graphics.DrawLine(new Pen(Brushes.Black, 2), new Point(drawRectangle.X,drawRectangle.Height),new Point(drawRectangle.Width,drawRectangle.Height));
            Font f = new Font("Tahoma", 9);
            if (m_Values != null && m_Values.Count > 1)
            {
                pe.Graphics.DrawString(m_Values[m_Values.Count - 1].ToString("N2"), f, Brushes.Black, new PointF(drawRectangle.X + 5, drawRectangle.Y));
                pe.Graphics.DrawString(m_Values.Count.ToString(), f, Brushes.Black, new PointF(drawRectangle.Width - TextRenderer.MeasureText(m_Values.Count.ToString(), f).Width, drawRectangle.Height - 5 - TextRenderer.MeasureText(m_Values.Count.ToString(), f).Height));
                calcMinMax();
                int startindex = Math.Max(1, m_Values.Count - drawRectangle.Width);
                Point start = new Point();
                Point stop = new Point();
                start.X = drawRectangle.X;
                start.Y = (int) Math.Max((((float)drawRectangle.Height) - ((float)drawRectangle.Height - 10) / (m_Max - m_Min) * (m_Values[0] - m_Min)), 0);
                for (int i = startindex; i < m_Values.Count; i++)
                {
                    stop.X = (int)Math.Max((i * ((drawRectangle.Width - 10) / ((float)m_Values.Count - 1))) + 10, 0);
                    stop.Y = (int) Math.Max((((float)drawRectangle.Height) - ((float)drawRectangle.Height - 10) / (m_Max - m_Min) * (m_Values[i] - m_Min)), 0);
                    pe.Graphics.DrawLine(new Pen(Brushes.Black, 1), start, stop);
                    start = stop;
                }
            }
            base.OnPaint(pe);
        }
    }
}
